﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Globalization;

namespace OverlakeApp.Converters
{
    /// <summary>
    /// A converter that is meant to be bound to a ListBox so that if the listbox is empty another object is shown.
    /// </summary>
    public class ListboxVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Converts from a data source for a listbox to a visibiltiy so that if the listbox is empty, another object can be shown.
        /// </summary>
        /// <param name="value">The source that the ListBox is bound to</param>
        /// <param name="targetType">Not used.</param>
        /// <param name="parameter">Not used.</param>
        /// <param name="culture">Not used.</param>
        /// <returns>An object of type System.Windows.Visibility.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Visibility.Visible;
            }
            else
            {
                IEnumerable<object> i = (value as IEnumerable).Cast<object>();
                if (i.Count() == 0)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
            }

            //return value == null ? Visibility.Visible : (value as IEnumerable<object>).Count() == 0 ? Visibility.Visible : Visibility.Collapsed;
        }

        /// <summary>
        /// Not implemented.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
